import logging
from google.appengine.ext import db

class SystemKeyValuePair(db.Model):
    system_key = db.StringProperty(required=True)    
    value = db.IntegerProperty(required=True)
    
    @staticmethod
    def get(system_key):
        system_key_value_pair_key = db.Key.from_path(
            'SystemKeyValuePair', system_key)
        system_key_value_pair = db.get(system_key_value_pair_key)

        if not system_key_value_pair:
            system_key_value_pair = SystemKeyValuePair(
                key_name = system_key, system_key=system_key, value=0)
            system_key_value_pair.put()
            logging.info('Initialized %s to 0.' % system_key)
        return system_key_value_pair

    @staticmethod
    def set(system_key, value):
        system_key_value_pair = SystemKeyValuePair(
            key_name = system_key, system_key=system_key, value=value)
        system_key_value_pair.put()

    def increment(self):
        self.value += 1
        self.put()
        return self